//公共方法集合
var ash ={
	showEditRow: function(){
		var content = $('.editrow');
		content.slideDown(500);
	},
	_datatable:{},
	initEdit:function(){
		//编辑按钮
		$(".dataEdit").each(function(){
			$(this).on("click",function(){
				$.ajax({
		            url : _ctx + "/admin/fang/person/"+$(this).attr("data"),
		            type : "get",
		            data : {},
		            dataType : "json",
		            success : function(returned) {
		            	if(returned.success){
		            		var data = returned.data;
						if(data.id)
						{
							$("#PersonForm input[name='id']").val(data.id);
						}
						else
							$("#PersonForm input[name='id']").val("");

						if(data.username)
						{
							$("#PersonForm input[name='username']").val(data.username);
						}
						else
							$("#PersonForm input[name='username']").val("");

						if(data.password)
						{
							$("#PersonForm input[name='password']").val(data.password);
						}
						else
							$("#PersonForm input[name='password']").val("");

						if(data.sex==0)
						{
							$("#PersonForm #sex").val(0);
						}
						else if(data.sex)
						{
							$("#PersonForm #sex").val(data.sex);
						}
						else
							$("#PersonForm #sex").val("-1");

						if(data.tel)
						{
							$("#PersonForm input[name='tel']").val(data.tel);
						}
						else
							$("#PersonForm input[name='tel']").val("");

						if(data.email)
						{
							$("#PersonForm input[name='email']").val(data.email);
						}
						else
							$("#PersonForm input[name='email']").val("");

						if(data.address)
						{
							$("#PersonForm input[name='address']").val(data.address);
						}
						else
							$("#PersonForm input[name='address']").val("");

			            	ash.showEditRow();
		            	}
		            	else{
		            		layer.msg("数据获取失败.请联系系统管理员!");
		            	}
		            	
		            },
		            erorr : function(e) {
		            	layer.msg(e.message());	
		            }
		        });
			});	
		});	
	}
};

//頁面加載完畢執行的方法
$(function() {
	//ztree 加載方法
	/*
	$.fn.zTree.init($("#personTree"), ztree.settings);
	$("#personPname").click(function(){
		ztree.show();
	});
	*/
	
	//加载数据
	ash._datatable = $(".personDataTable").dataTable(
					{
						language : {
							"sProcessing" : "处理中...",
							"sLengthMenu" : "显示 _MENU_ 项结果",
							"sZeroRecords" : "没有匹配结果",
							"sInfo" : "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
							"sInfoEmpty" : "显示第 0 至 0 项结果，共 0 项",
							"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
							"sInfoPostFix" : "",
							"sSearch" : "搜索:",
							"sUrl" : "",
							"sEmptyTable" : "表中数据为空",
							"sLoadingRecords" : "载入中...",
							"sInfoThousands" : ",",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "上页",
								"sNext" : "下页",
								"sLast" : "末页"
							},
							"oAria" : {
								"sSortAscending" : ": 以升序排列此列",
								"sSortDescending" : ": 以降序排列此列"
							}
						},
						serverSide : true,
						"ajax" : _ctx + "/admin/fang/person/data.json",
						"columns" : [
								{
									"data" : "id",
									"width" : "4%",
									"render" : function(data) {
										return "<input type='checkbox' class='selectable-checkbox' name='id' data='"+data+"'>";
									}
								},
								{
									"data" : "id"
								},
								{
									"data" : "username"
								},
								{
									"data" : "sex",
									"render" : function(data) {
										if(data===0)
											return "男";
										else if(data==1)
											return "女";
										else
											return "保密";
									}
								},
								{
									"data" : "tel"
								},
								{
									"data" : "email"
								},
								{
									"data" : "address"
								},
																{
									"data" : "id",
									"width" : "24%",
									"render" : function(data) {
										return "<a class='btn btn-xs btn-info dataEdit' data='"+data+"'><i class='fa fa-edit'></i>编辑</a> "+
										   "<a class='btn btn-xs btn-warning' data='"+data+"' href='"+_ctx+"/admin/fang/personGuanzhu/list?personid="+data+"'><i class='fa fa-building-o'></i>关注</a> "+
										   "<a class='btn btn-xs btn-primary' data='"+data+"' href='"+_ctx+"/admin/fang/personKanfang/list?personid="+data+"'><i class='fa fa-jpy'></i>看房</a> "+
										   "<a class='btn btn-xs btn-danger' data='"+data+"' href='"+_ctx+"/admin/fang/personKanjia/list?personid="+data+"'><i class='fa fa-gift'></i>砍价</a> "+
										   "<a class='btn btn-xs btn-info' data='"+data+"' href='"+_ctx+"/admin/fang/personYouhui/list?personid="+data+"'><i class='fa fa- image'></i>优惠</a> ";
									}
								}, ],
						deferRender : true
					});
	$('.personDataTable').on('draw.dt',function() {
		ash.initEdit();
	});
	
	//全选按钮
	$(".checkAll").on("click",function(){
		$("input:checkbox[name='id']").each(function(){
			$(this).prop("checked","checked");
		});
	});
	//反选按钮
	$(".toggleCheck").on("click",function(){
		$("input:checkbox[name='id']").each(function(){
			if($(this).prop("checked"))
				$(this).prop("checked",false);
			else
				$(this).prop("checked",true);
		});
	});
	//新增按钮
	$(".addBtn").on("click",function(){
				$(".validate input[name='id']").val(""),		
				$(".validate input[name='username']").val(""),		
				$(".validate input[name='password']").val(""),		
				$(".validate select[name='sex']").val(""),		
				$(".validate input[name='tel']").val(""),		
				$(".validate input[name='email']").val(""),		
				$(".validate input[name='address']").val(""),		
				ash.showEditRow();
	});
	//删除按钮
	$(".delBtn").on("click",function(){
		 var checkedIds = "";
	        var _checked = $("input.selectable-checkbox:checked");
	        if(_checked.length==0)
	        {
	        	layer.msg("<H4><i class='fa fa-exclamation-circle'></i>警告!</H4><p>请勾选至少一条记录!</p>");
	        }
	        for (var i = 0; i < _checked.length; i++) {
	            checkedIds += $(_checked[i]).attr("data") + ",";
	        }
	        $.ajax({
	            url : _ctx + "/admin/fang/person/delete",
	            type : "post",
	            data : {
	                id : checkedIds
	            },
	            dataType : "json",
	            success : function(returned) {
	            	layer.msg("<H4><i class='fa fa-check'></i></H4><p>删除成功!</p>");
	               	ash._datatable.fnDraw(); 
	               //	$.fn.zTree.init($("#personTree"), ztree.settings);
	            },
	            erorr : function(e) {
	            	layer.msg(e.message());	
	            }
	        });
	});
	$(".resetAddForm").on("click",function(){
		$("#PersonForm")[0].reset();
		$(".editrow").slideUp(500);
	})
	$(".validate").validate({
		submitHandler: function(form) {
			$.ajax({
				url:$(form).attr("action"),
				type:$(form).attr("method"),
				data:{
					id:$(".validate input[name='id']").val(),		
					username:$(".validate input[name='username']").val(),		
					password:$(".validate input[name='password']").val(),		
					sex:$(".validate select[name='sex']").val(),		
					tel:$(".validate input[name='tel']").val(),		
					email:$(".validate input[name='email']").val(),		
					address:$(".validate input[name='address']").val(),		
									},
				datatype:"json",
				success:function(returned){
					if(returned.success)
					{
						layer.msg(returned.msg);
					}
					else
						layer.msg(returned.msg);
					$("#PersonForm")[0].reset();
					$(".editrow").slideUp(500);
					ash._datatable.fnDraw(); 
					//$.fn.zTree.init($("#personTree"), ztree.settings);
				},
				error:function(error){
					layer.msg(error);
				}
			})
		}
	});
});